/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.managedbean;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.entity.DailyMPSTableEntity;
import mrp.productionPlanning.entity.MPSTableEntity;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import mrp.productionPlanning.session.DailyDetailSessionBean;
import mrp.productionPlanning.session.DailyMPSTableSessionBean;
import mrp.productionPlanning.session.MPSTableSessionBean;
import org.primefaces.event.FlowEvent;

/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class EditDailyMPSTableManagedBean {

    private static Logger logger = Logger.getLogger(EditMPSTableManagedBean.class.getName());
    private List<DailyMPSTableEntity> dailyMpsListA; // = new ArrayList<MPSTableEntity>();
    private List<DailyMPSTableEntity> dailyMpsListB;// = new ArrayList<MPSTableEntity>();
    private List<DailyMPSTableEntity> dailyMpsListC;// = new ArrayList<MPSTableEntity>();
    private List<DailyMPSTableEntity> dailyMpsListD;
    private int amountBalanceA;
    private int amountBalanceB;
    private int amountBalanceC;
    private int amountBalanceD;
    private int yearName;
    private String monthName = null;
//    @EJB
//    private InventoryControlSessionBean icsb = new InventoryControlSessionBean();
    @EJB
    MPSTableSessionBean mpsTableSessionBean;
    @EJB
    DailyMPSTableSessionBean dailyMPSTableSessionBean;
    @EJB
    DailyDetailSessionBean dailyDetailSessionBean;

    /** Creates a new instance of EditDailyMPSTableManagedBean */
    public EditDailyMPSTableManagedBean() {
    }

    public void initView(PhaseEvent event) {
        if (getDailyMpsListA() == null) {
            setDailyMpsListA((List<DailyMPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dailyMpsListA"));
            
        }
        if (getDailyMpsListB() == null) {
            setDailyMpsListB((List<DailyMPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dailyMpsListB"));

        }
        if (getDailyMpsListC() == null) {
            setDailyMpsListC((List<DailyMPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dailyMpsListC"));

        }
        if (getDailyMpsListD() == null) {
            setDailyMpsListD((List<DailyMPSTableEntity>) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dailyMpsListD"));

        }
        if (getYearName() == 0) {
            setYearName(getDailyMpsListA().get(0).getYearName());
        }
        if (getMonthName() == null) {
            setMonthName(getDailyMpsListA().get(0).getMonthName());
        }

    }

    public void updateA() {
        if(amountBalanceA !=0){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Total Amount dose not equal with the monthly amount. Please revise the data.", ""));
            return;
        }
        for (DailyMPSTableEntity dailyMpsA : dailyMpsListA) {
            Date d = dailyMpsA.getDay();
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            d.setDate(01);
            MonthlyDetailEntity monthlyDetail = dailyMpsA.getMonthlyDetailEntity();
            DailyDetailEntity dailyDetailEntity = dailyMpsA.getDailyDetailEntity();

            MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productA");
            
//            int day_year = c.get(Calendar.DAY_OF_YEAR);
            int day_forecastAmount = dailyMpsA.getProductionDemand();
            int currentInventory = mPSTableEntity.getInventory();
            List<DailyDetailEntity> listOfDays = dailyMPSTableSessionBean.sortList(monthlyDetail.getDays());
         
            if (c.getTime().equals(listOfDays.get(0).getDate())) {
                int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleA();
                dailyMpsA.setInventory(dailyInventoryLevel);
            } else {
                DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity) - 1);
                Date lastDay = lastDailyDetail.getDate();
 
                DailyMPSTableEntity lastDaily = dailyMPSTableSessionBean.getDailyMPSTableEntityByDate(lastDay, "productA");
                int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleA();
                dailyMpsA.setInventory(dailyInventoryLevel);
//                dailyDetailSessionBean.updateDailyDetail(lastDailyDetail);
                
            }
            dailyMpsA.setProductionDemand(dailyDetailEntity.getScheduleA());
            dailyMPSTableSessionBean.saveChanges(dailyMpsA);
        }
    }

    public void updateB() {
      if(amountBalanceB !=0){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Total Amount dose not equal with the monthly amount. Please revise the data.", ""));
            return;
        }
        for (DailyMPSTableEntity dailyMpsB : dailyMpsListB) {
            Date d = dailyMpsB.getDay();
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            d.setDate(01);
            MonthlyDetailEntity monthlyDetail = dailyMpsB.getMonthlyDetailEntity();
            DailyDetailEntity dailyDetailEntity = dailyMpsB.getDailyDetailEntity();
            MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productB");
            int day_forecastAmount = dailyMpsB.getProductionDemand();
            int currentInventory = mPSTableEntity.getInventory();
            List<DailyDetailEntity> listOfDays = dailyMPSTableSessionBean.sortList(monthlyDetail.getDays());
            if (c.getTime().equals(listOfDays.get(0).getDate())) {
                int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleB();
                dailyMpsB.setInventory(dailyInventoryLevel);
            } else {
                DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity) - 1);
                Date lastDay = lastDailyDetail.getDate();
                DailyMPSTableEntity lastDaily = dailyMPSTableSessionBean.getDailyMPSTableEntityByDate(lastDay, "productB");
                int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleB();
                dailyMpsB.setInventory(dailyInventoryLevel);
               
            }
             dailyMpsB.setProductionDemand(dailyDetailEntity.getScheduleA());
             dailyMPSTableSessionBean.saveChanges(dailyMpsB);
        }
    }

    public void updateC() {
       if(amountBalanceC !=0){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Total Amount dose not equal with the monthly amount. Please revise the data.", ""));
            return;
        }
        for (DailyMPSTableEntity dailyMpsC : dailyMpsListC) {
            Date d = dailyMpsC.getDay();
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            d.setDate(01);
            MonthlyDetailEntity monthlyDetail = dailyMpsC.getMonthlyDetailEntity();
            DailyDetailEntity dailyDetailEntity = dailyMpsC.getDailyDetailEntity();
            MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productC");
            int day_forecastAmount = dailyMpsC.getProductionDemand();
            int currentInventory = mPSTableEntity.getInventory();
            List<DailyDetailEntity> listOfDays = dailyMPSTableSessionBean.sortList(monthlyDetail.getDays());
            if (c.getTime().equals(listOfDays.get(0).getDate())) {
                int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleC();
                dailyMpsC.setInventory(dailyInventoryLevel);
            } else {
                DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity) - 1);
                Date lastDay = lastDailyDetail.getDate();
               
                DailyMPSTableEntity lastDaily = dailyMPSTableSessionBean.getDailyMPSTableEntityByDate(lastDay, "productC");
                int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleC();
                dailyMpsC.setInventory(dailyInventoryLevel);
                
            }
            dailyMpsC.setProductionDemand(dailyDetailEntity.getScheduleA());
            dailyMPSTableSessionBean.saveChanges(dailyMpsC);
        }
    }

    public void updateD() {
       if(amountBalanceD !=0){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "The Total Amount dose not equal with the monthly amount. Please revise the data.", ""));
            return;
        }
        for (DailyMPSTableEntity dailyMpsD : dailyMpsListD) {
            Date d = dailyMpsD.getDay();
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            d.setDate(01);
            MonthlyDetailEntity monthlyDetail = dailyMpsD.getMonthlyDetailEntity();
            DailyDetailEntity dailyDetailEntity = dailyMpsD.getDailyDetailEntity();
            MPSTableEntity mPSTableEntity = mpsTableSessionBean.getMPSTableByDate(d, "productD");
            int day_forecastAmount = dailyMpsD.getProductionDemand();
            int currentInventory = mPSTableEntity.getInventory();
            List<DailyDetailEntity> listOfDays = dailyMPSTableSessionBean.sortList(monthlyDetail.getDays());
            if (c.getTime().equals(listOfDays.get(0).getDate())) {
                int dailyInventoryLevel = currentInventory - day_forecastAmount + dailyDetailEntity.getScheduleD();
                dailyMpsD.setInventory(dailyInventoryLevel);
            } else {
                DailyDetailEntity lastDailyDetail = listOfDays.get(listOfDays.indexOf(dailyDetailEntity) - 1);
                Date lastDay = lastDailyDetail.getDate();
                DailyMPSTableEntity lastDaily = dailyMPSTableSessionBean.getDailyMPSTableEntityByDate(lastDay, "productD");
                int dailyInventoryLevel = lastDaily.getInventory() - day_forecastAmount + dailyDetailEntity.getScheduleD();
                dailyMpsD.setInventory(dailyInventoryLevel);
               
            }
            dailyMpsD.setProductionDemand(dailyDetailEntity.getScheduleA());
             dailyMPSTableSessionBean.saveChanges(dailyMpsD);
        }
    }
    public void calculateAmountA(ActionEvent event){
        int totalAmount = dailyMpsListA.get(0).getMonthlyDetailEntity().getScheduleA();
        amountBalanceA = totalAmount;
        for (DailyMPSTableEntity dailyMpsA : dailyMpsListA){
            amountBalanceA -= dailyMpsA.getDailyDetailEntity().getScheduleA();
        }
    }
        public void calculateAmountB(ActionEvent event){
        int totalAmount = dailyMpsListB.get(0).getMonthlyDetailEntity().getScheduleB();
        amountBalanceB = totalAmount;
        for (DailyMPSTableEntity dailyMpsB : dailyMpsListB){
            amountBalanceB -= dailyMpsB.getDailyDetailEntity().getScheduleB();
        }
    }
        public void calculateAmountC(ActionEvent event){
        int totalAmount = dailyMpsListC.get(0).getMonthlyDetailEntity().getScheduleC();
        amountBalanceC = totalAmount;
        for (DailyMPSTableEntity dailyMpsC : dailyMpsListC){
            amountBalanceC -= dailyMpsC.getDailyDetailEntity().getScheduleC();
        }
    }
        public void calculateAmountD(ActionEvent event){
        int totalAmount = dailyMpsListD.get(0).getMonthlyDetailEntity().getScheduleD();
        amountBalanceD = totalAmount;
        for (DailyMPSTableEntity dailyMpsD : dailyMpsListD){
            amountBalanceD -= dailyMpsD.getDailyDetailEntity().getScheduleD();
        }
    }
    public void save() {
        updateA();
        updateB();
        updateC();
        updateD();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Daily MPS is Successfully Updated.", ""));
    }
    public String onFlowProcess(FlowEvent event) {
        logger.info("Current wizard step:" + event.getOldStep());
        logger.info("Next step:" + event.getNewStep());

        return event.getNewStep();
    }

    public List<DailyMPSTableEntity> getDailyMpsListA() {
        return dailyMpsListA;
    }

    public void setDailyMpsListA(List<DailyMPSTableEntity> dailyMpsListA) {
        this.dailyMpsListA = dailyMpsListA;
    }

    public List<DailyMPSTableEntity> getDailyMpsListB() {
        return dailyMpsListB;
    }

    public void setDailyMpsListB(List<DailyMPSTableEntity> dailyMpsListB) {
        this.dailyMpsListB = dailyMpsListB;
    }

    public List<DailyMPSTableEntity> getDailyMpsListC() {
        return dailyMpsListC;
    }

    public void setDailyMpsListC(List<DailyMPSTableEntity> dailyMpsListC) {
        this.dailyMpsListC = dailyMpsListC;
    }

    public List<DailyMPSTableEntity> getDailyMpsListD() {
        return dailyMpsListD;
    }

    public void setDailyMpsListD(List<DailyMPSTableEntity> dailyMpsListD) {
        this.dailyMpsListD = dailyMpsListD;
    }

    public int getYearName() {
        return yearName;
    }

    public void setYearName(int yearName) {
        this.yearName = yearName;
    }

    public String getMonthName() {
        return monthName;
    }

    public void setMonthName(String monthName) {
        this.monthName = monthName;
    }

    public int getAmountBalanceA() {
        return amountBalanceA;
    }

    public void setAmountBalanceA(int amountBalanceA) {
        this.amountBalanceA = amountBalanceA;
    }

    public int getAmountBalanceB() {
        return amountBalanceB;
    }

    public void setAmountBalanceB(int amountBalanceB) {
        this.amountBalanceB = amountBalanceB;
    }

    public int getAmountBalanceC() {
        return amountBalanceC;
    }

    public void setAmountBalanceC(int amountBalanceC) {
        this.amountBalanceC = amountBalanceC;
    }

    public int getAmountBalanceD() {
        return amountBalanceD;
    }

    public void setAmountBalanceD(int amountBalanceD) {
        this.amountBalanceD = amountBalanceD;
    }
}
